<?php
class Admin_CustomerController extends Apps_Controller_Action_Admin {
    public function indexAction() {
		$this->_helper->redirector('list', null, null);
    }

    public function listAction() {
		$this->actResolve('act_delete',
						  'delete', null, null, array('id' => $this->_getParam('item_checker')),
					 	  true);

		$tbCustomer = new M_DbTable_Customer();
		$grid 	= new Apps_Plugin_Grid('cruiseGrid');

		$grid->setData($tbCustomer->getList())
			 ->setTableCaption('List Customer')
			 ->setTableColumnFormat(array(
			 		array('title'		=> 'ID',
			 			  'dataField'	=> 'id',
			 			  'class'		=> 'center'),
			 		array('title'		=> 'Full Name',
			 			  'width'		=> '10%',
			 			  'dataField'	=> 'full_name'),
			 		array('title'		=> 'Email',
			 			  'html'        => array('<a href="mailto:%s" target="_blank">%s</a>',
			 			  				  array('email', 'email')),),
			 		array('title'		=> 'Phone',
			 			  'dataField'	=> 'phone'),
			 		array('title'		=> 'Address',
			 			  'dataField'	=> 'address'),
			 		array('title'		=> 'Country',
			 			  'dataField'	=> 'country_residence'),
			 		array('title'       => 'Edit',
			 			  'html'        => array('<a href="/admin/customer/edit?id=%s"><i class="icon-edit" title="Sửa"></i></a>',
			 			  				   array('id')),
			 			  'width'       => '2%'),
			 		array('title'       => 'Delete',
			 			  'html'        => array('<a href="/admin/customer/delete?id=%s"><i class="icon-remove must-confirm" data-must-confirm-msg="Xóa TT khác hàng" title="Xóa"></i></a>',
			 			  				   array('id')),
			 			  'width'       => '2%')
			 		));
		$grid->applySearch(new Admin_Form_Search_Cruise());

		$this->view->grid = $grid;
    }

    public function addAction() {
		$rq = $this->_request;

		$tbCustomer = new M_DbTable_Customer();
		$f 	= new Admin_Form_AddCustomer();

		if ($rq->isPost()) {
			if ($f->isValid($rq->getParams())) {
				$this->_flashMessenger->addMessage(
					$tbCustomer->safelyInsert($f->getValues())
					? self::$msgAddSuccess
					: self::$msgAddFailed);

				$this->_helper->redirector('index', null, null);
			}
		}
		$this->view->form = $f;
    }

    public function editAction() {
		$rq = $this->_request;
        $id = $rq->getParam('id');
        $tbCustomer = new M_DbTable_Customer();

        if(!$id || !($r = $tbCustomer->fetchRow(array('id = ?' => $id))->toArray())) {
            $this->_flashMessenger->addMessage(self::$msgNoRecordFound);
            return $this->goToActionIndex();
        }

        $form = new Admin_Form_EditCustomer();

        if($rq->isPost()) {
            if($form->isValid($rq->getParams())) {
                $r = $tbCustomer->safelyUpdate($form->getValues(), array('id=?' => $id));
                $this->_flashMessenger->addMessage(
                	$r
                    ? self::$msgUpdateSuccess
                    : self::$msgUpdateFailed);
                return $this->goToActionIndex();
            }
        } else {
            $form->populate($r);
        }
		$form->setLegend('Edit Customer');	
        $this->view->form = $form;
    }

    public function deleteAction() {
		$this->disableLayout(true, true);

		$rq = $this->_request;
		$customersIds = $rq->getParam('id');
		$tbcustomers = new M_DbTable_Customer();

		if(is_numeric($customersIds)) {
			$this->_flashMessenger->addMessage(
						$tbcustomers->safelyDelete(array('id = ?' => $customersIds)) ?
						self::$msgDeleteSuccess :
						self::$msgDeleteError
					);
			$this->goToActionIndex();
		} elseif (is_array($customersIds)) {
			foreach ($customersIds as $customerId) {
				$tbcustomers->safelyDelete(array('id = ?' => $customerId));
			}
			$this->_flashMessenger->addMessage(self::$msgDeleteSuccess);
			$this->goToActionIndex();
		} else {
			$this->_flashMessenger->addMessage(self::$msgNoRecordFound);
			$this->goToActionIndex();
		}
	}
}